package com.cal.utms.dao.csefileupload;

import java.math.BigDecimal;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.cal.utms.entity.Csedownload;
import com.cal.utms.entity.Instrument;
import com.cal.utms.entity.Issuercodemapping;
import com.cal.utms.util.HibernateUtil;

public class CseFileUploadDaoImpl implements CseFileUploadDao {

	@SuppressWarnings("unchecked")
	public List<Csedownload> getUnMappingCode() {
		Session session = HibernateUtil.getSession().openSession();
		Transaction transaction = null;
		List<Csedownload> issuercodemappings = null;
		try {
			transaction = session.beginTransaction();
			issuercodemappings = session.createQuery("from Csedownload C where C.issuerMapping=:status GROUP BY C.issuerName").setParameter("status", "Not Mapped").list();
			transaction.commit();
		} catch (Exception e) {
			if (transaction != null)
				transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return issuercodemappings;
	}

	@SuppressWarnings("unchecked")
	public List<Csedownload> getAllRecords() {
		Session session = HibernateUtil.getSession().openSession();
		Transaction transaction = null;
		List<Csedownload> issuercodemappings = null;
		try {
			transaction = session.beginTransaction();
			issuercodemappings = session.createQuery("from Csedownload C ").list();
			transaction.commit();
		} catch (Exception e) {
			if (transaction != null)
				transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return issuercodemappings;
	}

	public void saveupdate(List<Csedownload> updateCsedownloads, List<Instrument> instruments) throws Exception {
		System.out.println("herrr");
		Session session = HibernateUtil.getSession().openSession();
		Transaction transaction = null;
		try {
			transaction = session.beginTransaction();
			for (Csedownload csedownload : updateCsedownloads) {
				session.merge(csedownload);
			}
			if (instruments.size() > 0) {
				for (Instrument instrument : instruments) {
					session.merge(instrument);
				}
			}
			transaction.commit();
		} catch (Exception e) {
			if (transaction != null)
				transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}

	}

	@SuppressWarnings("unchecked")
	public List<Issuercodemapping> getMappedCode() {
		Session session = HibernateUtil.getSession().openSession();
		Transaction transaction = null;
		List<Issuercodemapping> issuercodemappings = null;
		try {
			transaction = session.beginTransaction();
			issuercodemappings = session.createQuery("from Issuercodemapping C ").list();
			transaction.commit();
		} catch (Exception e) {
			if (transaction != null)
				transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return issuercodemappings;
	}

	public Csedownload getRecords(String isin, String issuerName, String currency, String issuerPrice, String couponType, BigDecimal couponRate, String paymentPeriod,
			String maturity, String dayCount, String redemptionTerm, Integer parValue, String dateIssued, BigDecimal issuedQuantity) {
		Session session = HibernateUtil.getSession().openSession();
		Transaction transaction = null;
		Csedownload csedownload = null;
		try {
			transaction = session.beginTransaction();
			csedownload = (Csedownload) session
					.createQuery(
							"from Csedownload C where C.isin=:isin and C.issuerName=:issuerName and C.currency=:currency and C.issuerPrice=:issuerPrice"
									+ " and C.couponType=:couponType and C.couponRate=:couponRate and C.paymentPeriod=:paymentPeriod and C.maturity=:maturity"
									+ " and C.dayCount=:dayCount and C.redemptionTerm=:redemptionTerm and C.parValue=:parValue and C.dateIssued=:dateIssued and C.issuedQuantity=:issuedQuantity")
					.setParameter("isin", isin).setParameter("issuerName", issuerName).setParameter("currency", currency).setParameter("issuerPrice", issuerPrice)
					.setParameter("couponType", couponType).setParameter("couponRate", couponRate).setParameter("paymentPeriod", paymentPeriod).setParameter("maturity", maturity)
					.setParameter("dayCount", dayCount).setParameter("redemptionTerm", redemptionTerm).setParameter("parValue", parValue).setParameter("dateIssued", dateIssued)
					.setParameter("issuedQuantity", issuedQuantity).uniqueResult();
			transaction.commit();
		} catch (Exception e) {
			if (transaction != null)
				transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return csedownload;
	}

	public Csedownload getbysymbol(String symbol) {
		Session session = HibernateUtil.getSession().openSession();
		Transaction transaction = null;
		Csedownload csedownload = null;
		try {
			transaction = session.beginTransaction();
			csedownload = (Csedownload) session.createQuery("from Csedownload C where C.symbol=:symbol").setParameter("symbol", symbol).uniqueResult();
			transaction.commit();
		} catch (Exception e) {
			if (transaction != null)
				transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return csedownload;
	}

	@SuppressWarnings("unchecked")
	public List<Csedownload> getCserecordByIssuerCode(String name) {
		Session session = HibernateUtil.getSession().openSession();
		Transaction transaction = null;
		List<Csedownload> issuercodemappings = null;
		try {
			transaction = session.beginTransaction();
			issuercodemappings = session.createQuery("from Csedownload C where C.status=:status and C.issuerName=:name").setParameter("status", "Not Complite")
					.setParameter("name", name).list();
			transaction.commit();
		} catch (Exception e) {
			if (transaction != null)
				transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return issuercodemappings;
	}

	public Object saveMapping(List<Issuercodemapping> issuercodemappings, List<Instrument> instruments, List<Csedownload> csedownloadsList) {
		Session session = HibernateUtil.getSession().openSession();
		Transaction transaction = null;
		Object object = null;
		try {
			transaction = session.beginTransaction();
			for (Issuercodemapping issuercodemapping : issuercodemappings) {
				object = session.save(issuercodemapping);
			}
			for (Csedownload csedownload : csedownloadsList) {
				object = session.merge(csedownload);
			}
			for (Instrument instrument : instruments) {
				object = session.save(instrument);
			}
			transaction.commit();
		} catch (Exception e) {
			if (transaction != null)
				transaction.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}

		return object;
	}
}
